
// 注意：微信小程序不直接支持axios，这里使用wx.request封装

// 创建请求实例
export const request = (config) => {
  // 基础配置
  const baseURL = 'http://localhost:8080'; // 后端服务地址
  const timeout = 30000; // 超时时间
  
  // 合并配置
  const requestConfig = {
    url: config.url,
    method: config.method || 'GET',
    data: config.data || {},
    header: {
      'Content-Type': 'application/json;charset=utf-8',
      ...config.headers
    },
    timeout: config.timeout || timeout,
    ...config
  };
  
  // 处理URL
  if (!requestConfig.url.startsWith('http')) {
    requestConfig.url = baseURL + requestConfig.url;
  }
  
  // 返回Promise
  return new Promise((resolve, reject) => {
    wx.request({
      ...requestConfig,
      success: (res) => {
        console.log("请求成功",res)
        resolve(res);
      },
      fail: (error) => {
        console.log("请求失败",error)
        reject(error);
      }
    });
  });
};

// 封装GET请求
request.get = (url, params = {}, config = {}) => {
  return request({
    url,
    method: 'GET',
    data: params,
    ...config
  });
};

// 封装POST请求
request.post = (url, data = {}, config = {}) => {
  return request({
    url,
    method: 'POST',
    data,
    ...config
  });
};
